package com.tanhua.dubbo.api.impl.mongo;
import com.tanhua.domain.vo.PageResult;
import org.bson.types.ObjectId;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.dubbo.api.mongo.FriendApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(timeout = 50000)
public class FriendApiImpl implements FriendApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long userId, Long friendId) {
        //需求：添加好友。往tanhua_users表注册好友的双向关系
        //db.tanhua_users.insert({userId:1,friendId:57})
        //db.tanhua_users.insert({userId:57,friendId:1})

        // 构造查询条件
        Query query = Query.query(
                Criteria.where("userId").is(userId)
                .and("friendId").is(friendId)
        );
        // 先判断: 如果userId=1与friendId=57不是好友才执行添加
        if (!mongoTemplate.exists(query, Friend.class)){
            Friend friend = new Friend();
            friend.setUserId(userId);
            friend.setFriendId(friendId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
        }

        //  再判断: 如果userId=57与friendId=1不是好友才执行添加
        query = Query.query(
                Criteria.where("userId").is(friendId)
                        .and("friendId").is(userId)
        );
        if (!mongoTemplate.exists(query, Friend.class)){
            Friend friend = new Friend();
            friend.setUserId(friendId);
            friend.setFriendId(userId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
        }
    }

    @Override
    public PageResult contactsList(Integer page, Integer pagesize, String keyword, Long userId) {
        // db.tanhua_users.find({userId:1}).sort({created:-1})
        // 构造条件
        Query query = Query.query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("created")));
        query.skip((page-1)*pagesize).limit(pagesize);
        // 分页查询
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        // 分页统计
        long count  = mongoTemplate.count(query, Friend.class);
        return new PageResult(page,pagesize, (int) count,friendList);
    }

    @Override
    public void delete(Long userId, Long friendId) {
        // 构造查询条件
        Query query = Query.query(
                Criteria.where("userId").is(userId)
                        .and("friendId").is(friendId)
        );
            mongoTemplate.remove(query,Friend.class);
        // 构造查询条件
         query = Query.query(
                Criteria.where("userId").is(friendId)
                        .and("friendId").is(userId)
        );
            mongoTemplate.remove(query,Friend.class);

    }


}
